﻿using DBAccessLibrary.Interface;
using Skynet.CommonLibrary.Extension;
using DBAccessLibrary.Model;
using DBAccessLibrary.Grammar;

namespace DBAccessLibrary
{
    /// <summary>
    /// 数据操作工厂
    /// </summary>
    public static class DBOperatorFactory
    {
        /// <summary>
        /// 获取所有数据操作接口
        /// </summary>
        public static IOperations GetOperator
        {
            get
            {
                IOperations op = HttpRuntimeCache.GetCache<DatabaseOperations>("DatabaseOperations");
                if (op == null)
                {
                    op = new DatabaseOperations();
                    HttpRuntimeCache.SetCache("DatabaseOperations", op, 30);
                }
                return op;
            }
        }

        /// <summary>
        /// 获取语句生成接口
        /// </summary>
        public static IGrammar GetGrammar(ConnectionModel connect)
        {
            switch (connect.ConnectType)
            {
                case DBType.DB2:
                    return new Db2Grammar(connect);
                case DBType.MongoDB:
                    return new MongoDbGrammar(connect);
                case DBType.ODBC:
                    return new OdbcGrammar(connect);
                case DBType.Ole:
                    return new OleGrammar(connect);
                case DBType.Oracle:
                    return new OracleGrammar(connect);
                case DBType.SqlServer:
                    return new SqlGrammar(connect);
            }
            return null;
        }

        internal static ILocalGrammar GetLocalGrammar(ConnectionModel connect)
        {
            return (ILocalGrammar)GetGrammar(connect);
        }
    }
}
